<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="stylesheet" href="./../layui/css/layui.css" media="all">
    <script src="./../layui/layui.js"></script>
    <script src="./../js/jquery-1.8.3.min.js"></script>
    <script src="./../js/env.js"></script>
    <title>充电站管理</title>
</head>
<fieldset class="layui-elem-field layui-field-title">
    <legend>充电站管理</legend>
</fieldset>
<div class="demoTable">
    <button type="button" class="layui-btn" style="margin-left: 10px;margin-bottom: 10px" onclick="javascript:window.location.href='./add/charg_add.html'">添加充电站</button>
</div>

<form method="get" action="" class="layui-form">
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">充电站名字</label>
            <div class="layui-input-inline">
                <input id="stationName" autocomplete="off" class="layui-input" type="text">
            </div>
        </div>
        <button class="layui-btn" onclick="sech()" data-method="offset" type="button">搜索</button>
        <button type="reset" onclick="chong()" data-method="offset" class="layui-btn">重置</button>
    </div>
</form>
<body>
<table id="demo" lay-filter="test"></table>
</body>

</html>
<script type="text/html" id="barDemo">
    <button type="button" onclick="chargInfo('{{d.id}}')" class="layui-btn layui-btn-sm layui-btn-normal"
            lay-event="edit" style="background: #4b945f">管理充电桩
    </button>
    <button type="button" onclick="editCharge('{{d.id}}')" class="layui-btn layui-btn-sm layui-btn-normal"
            lay-event="edit" style="background: dodgerblue">编辑
    </button>
    <button type="button" onclick="deleteCharge('{{d.id}}')" class="layui-btn layui-btn-sm layui-btn-normal"
            lay-event="edit" style="background: red">删除
    </button>
</script>
<script>
    if (layui.sessionData('user').userInfo === undefined) {
        window.location.href = "../html/login.html"
    }
    layui.use('table', function () {
        var table = layui.table;

        var whereData = {}
        whereData.stationName = $("#stationName").val();

        //第一个实例
        table.render({
            elem: '#demo'
            , id: 'testReload'
            , where: whereData
            , headers: {sessionId: getToken()}
            , url: abc() + '/admin/chargeStation/selectAllChargingStations' //数据接口
            , page: true //开启分页
            , cols: [[ //表头
                {field: 'id', title: 'id', sort: true, fixed: 'left', align: 'center'}
                , {field: 'longitude', title: '经度', align: 'center'}
                , {field: 'dimensionality', title: '维度', align: 'center'}
                , {field: 'stationName', title: '充电站名字', align: 'center'}
                , {field: 'stationDetails', title: '充电站详情', align: 'center'}
                , {field: 'province', title: '地区', align: 'center'}
                , {field: 'address', title: '详细地址', align: 'center'}
                , {field: 'price', title: '价格元/度', align: 'center'}
                , {field: 'parkingFeeDetails', title: '停车费', align: 'center'}
                , {
                    field: 'haveParkingFee', title: '有无停车费', templet: function (val) {
                        if (val.haveParkingFee == 0) {
                            return "无";
                        } else {
                            return "有";
                        }
                    }, align: 'center'
                }
                , {field: 'openStartTime', title: '开放开始时间 HH:mm:ss', align: 'center'}
                , {field: 'openEndTime', title: '开放结束时间 HH:mm:ss', align: 'center'}
                , {
                    field: 'coverImg', title: '封面图', templet: function (val) {
                        return "<img src='" + abc() + val.coverImg + "'>";
                    }, align: 'center'
                }
                , {field: 'createTime', title: '创建时间', align: 'center'}
                , {field: 'right', title: '操作', width: 250, toolbar: '#barDemo', align: 'center'}
            ]]
            , request: {
                pageName: 'page' //页码的参数名称，默认：page
                , limitName: 'pageSize' //每页数据量的参数名，默认：limit
            }
            , done: function (res, curr, count) {
                if (res.code === 100) {
                    layer.msg("登录失效，请重新登录")
                    window.location.href = "../html/login.html"
                    return;
                }
            }
            , parseData: function (res) { //res 即为原始返回的数据
                if (res.code !== 0) {
                    return {
                        "code": res.code, //解析接口状态
                        "msg": res.msg //解析提示文本
                    };
                } else {
                    return {
                        "code": res.code, //解析接口状态
                        "msg": res.msg, //解析提示文本
                        "count": res.data.total, //解析数据长度
                        "data": res.data.list //解析数据列表
                    };
                }

            }
        });

    });

    function sech() {
        var whereData = {};
        whereData.stationName = $("#stationName").val();
        //表刷新方法
        var table = layui.table;
        table.reload('testReload', {
            url: abc() + '/admin/chargeStation/selectAllChargingStations'
            , where: whereData
        });

    }

    //把重置添加一个点击事件点击重置刷新页面
    function chong() {
        window.location.reload();
    }

    //删除
    function deleteCharge(id) {
        layer.confirm('确定删除?', function (index) {
            var param = {};
            param.id = id;
            var respones = MyAjax("POST", "/admin/chargeStation/delStations", param, getToken());
            if (respones.code === 0) {
                layer.msg("删除成功");
                sech();
            }
        });

    }

    //管理充电桩
    function chargInfo(id) {
        window.location.href = "./charg_info.html?id=" + id;
    }

    //编辑
    function editCharge(id) {
        window.location.href = "./edit/charg_edit.html?id=" + id;
    }
</script>